package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qf.mapper.DtsGoodsSpecificationMapper;
import com.qf.pojo.DtsGoodsAttribute;
import com.qf.pojo.DtsGoodsProduct;
import com.qf.pojo.DtsGoodsSpecification;
import com.qf.service.DtsGoodsSpecificationServic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author YDW
 * projectName:shop-manage
 * time:2022/5/22 23:32
 * description:
 */
@Service
public class DtsGoodsSpecificationServicImpl implements DtsGoodsSpecificationServic {

    @Autowired
    DtsGoodsSpecificationMapper specificationMapper;
    @Override
    public void updateSpecifications(Integer id, DtsGoodsSpecification[] specifications) {

        //根据商品id删除规格记录
        LambdaQueryWrapper<DtsGoodsSpecification> qw=new LambdaQueryWrapper<>();
        qw.eq(DtsGoodsSpecification::getGoodsId,id);
        specificationMapper.delete(qw);

        //添加规格
        if(specifications!=null&&specifications.length>0){
            for (DtsGoodsSpecification specification : specifications) {
                specification.setAddTime(new Date());
                specification.setUpdateTime(new Date());
                specification.setGoodsId(id);
                specification.setDeleted(false);
                specificationMapper.insert(specification);
            }
        }
    }

    @Override
    public void deteleByGoodsId(Integer id) {
        DtsGoodsSpecification specification=new DtsGoodsSpecification();
        specification.setDeleted(true);
        LambdaQueryWrapper<DtsGoodsSpecification> qw=new LambdaQueryWrapper<>();
        qw.eq(DtsGoodsSpecification::getGoodsId,id);
        specificationMapper.update(specification,qw);
    }

    @Override
    public void addSpecification(Integer id, DtsGoodsSpecification[] specifications) {
        //添加规格
        if(specifications!=null&&specifications.length>0){
            for (DtsGoodsSpecification specification : specifications) {
                specification.setAddTime(new Date());
                specification.setUpdateTime(new Date());
                specification.setGoodsId(id);
                specification.setDeleted(false);
                specificationMapper.insert(specification);
            }
        }
    }
}
